<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2024/7/3
 * Time: 8:49
 */

namespace App\Admin\Services;


use App\Models\ErpRepairSettlement;
use App\Models\ErpRwoRepairItemEntry;
use App\Models\ErpRwoSparepartEntry;
use Illuminate\Support\Facades\DB;

class ErpRepairSettlementService
{
    /**
     * 接车台次
     * @param array $condition
     * @return mixed
     */
    public static function erpMeetCarCount($condition = [])
    {
        $date = datetimeFormatter($condition['target_date']);

        $count = ErpRepairSettlement::where('FSettlePersonID', $condition['user_erp_id'])
            ->where('FRepairTypeID', '!=', ERP_REPAIR_TYPE_FOUR)
            ->where('FRepairTypeID', '!=', ERP_REPAIR_TYPE_THREE)
            ->whereBetween('FSettleDate', [$date['start_time'], $date['end_time']])
            ->where('FStatus', ERP_ORDER_STATUS_SETTLEMENT)
            ->where('FSourceBillTypeID', ERP_SCM_BILLTYPE)
            ->where('FOrgUnitID', $condition['store_erp_id'])
            ->count();

        return $count;
    }

    /**
     * 产值实际
     * @param array $condition
     * @return mixed
     */
    public static function erpRealOutputValue($condition = [])
    {
        $date = datetimeFormatter($condition['target_date']);

        $sum_amount = ErpRepairSettlement::where('FSettlePersonID', $condition['user_erp_id'])
            ->where('FRepairTypeID', '!=', ERP_REPAIR_TYPE_FOUR)
            ->whereBetween('FSettleDate', [$date['start_time'], $date['end_time']])
            ->where('FStatus', ERP_ORDER_STATUS_SETTLEMENT)
            ->where('FSourceBillTypeID', ERP_SCM_BILLTYPE)
            ->where('FOrgUnitID', $condition['store_erp_id'])
            ->sum('FSettleAmount');

        return $sum_amount;
    }

    /**
     * 定保台次实际
     * @param array $condition
     * @return mixed
     */
    public static function erpRealMaintenanceVehicle($condition = [])
    {
        $date = datetimeFormatter($condition['target_date']);

        $count = ErpRepairSettlement::where('FSettlePersonID', $condition['user_erp_id'])
            ->where('FRepairTypeID', '=', ERP_REPAIR_TYPE_TWO)
            ->whereBetween('FSettleDate', [$date['start_time'], $date['end_time']])
            ->where('FStatus', ERP_ORDER_STATUS_SETTLEMENT)
            ->where('FSourceBillTypeID', ERP_SCM_BILLTYPE)
            ->where('FOrgUnitID', $condition['store_erp_id'])
            ->count();

        return $count;
    }

    /**
     * 机修工时
     * @param array $condition
     * @return mixed
     */
    public static function erpMachineRepairWorkingHours($condition = [])
    {
        $date = datetimeFormatter($condition['target_date']);

//        $sum_time = ErpRepairSettlement::where('FSettlePersonID', $condition['user_erp_id'])
//            ->where('FRepairTypeID', '=', ERP_REPAIR_TYPE_TWO)
//            ->whereBetween('FSettleDate', [$date['start_time'], $date['end_time']])
//            ->where('FStatus', ERP_ORDER_STATUS_SETTLEMENT)
//            ->where('FSourceBillTypeID', ERP_SCM_BILLTYPE)
//            ->where('FOrgUnitID', $condition['store_erp_id'])
//            ->count();

//        $sum_time = ErpRwoRepairItemEntry::where('FPersonID', $condition['user_erp_id'])
//            ->where('FRepairTypeID', '=', ERP_REPAIR_TYPE_TWO)
//            ->whereBetween('FActualFinishTime', [$date['start_time'], $date['end_time']])
//            ->where('FItemStatus', ERP_ITEM_STATUS_SIX)
//            ->sum('FActualWorkTime');
////            ->count();
//        dd($sum_time);

//        $sum_time = ErpRwoRepairItemEntry::where('FPersonID', $condition['user_erp_id'])
//            ->where('FRepairTypeID', '=', ERP_REPAIR_TYPE_TWO)
//            ->whereBetween('FActualFinishTime', [$date['start_time'], $date['end_time']])
//            ->where('FItemStatus', ERP_ITEM_STATUS_SIX)
//            ->sum('FActualWorkTime');
//        dd($condition);
        $sum_time = ErpRwoRepairItemEntry::whereHas('erpRepairSettlement', function ($query) use ($condition, $date) {
            $query->whereBetween('FSettleDate', [$date['start_time'], $date['end_time']]);
            $query->where('FRepairTypeID', '=', ERP_REPAIR_TYPE_TWO);
            $query->where('FStatus', ERP_ORDER_STATUS_SETTLEMENT);
            $query->where('FSourceBillTypeID', ERP_SCM_BILLTYPE);
            $query->where('FOrgUnitID', $condition['store_erp_id']);
        })
        ->where('FPersonID', $condition['user_erp_id'])
        ->where('FItemStatus', ERP_ITEM_STATUS_SIX)
        ->sum('FActualWorkTime');
//        ->count();

        return $sum_time;
    }

    /**
     * 追加产值
     * @param array $condition
     * @return mixed
     */
    public static function erpAppendValue($condition = [])
    {
        $date = datetimeFormatter($condition['target_date']);

        $amount = ErpRwoSparepartEntry::whereHas('erpRepairSettlement', function ($query) use ($condition, $date) {
            $query->whereBetween('FSettleDate', [$date['start_time'], $date['end_time']]);
            $query->where('FRepairTypeID', '=', ERP_REPAIR_TYPE_TWO);
            $query->where('FStatus', ERP_ORDER_STATUS_SETTLEMENT);
            $query->where('FSourceBillTypeID', ERP_SCM_BILLTYPE);
            $query->where('FOrgUnitID', $condition['store_erp_id']);
        })->whereHas('erpRwoRepairItemEntry', function ($query) use ($condition, $date) {
            $query->where('FPersonID', $condition['user_erp_id']);
            $query->where('FItemStatus', ERP_ITEM_STATUS_SIX);
        })
        ->whereNotIn('FMATERIALID', mergeErpRepairSparepartNot())
        ->sum('FTaxAmount');

        return $amount;
    }

    public static function erpRepairRealOutputValue($condition = [])
    {
        $date = datetimeFormatter($condition['target_date']);

        $amount = ErpRwoSparepartEntry::whereHas('erpRepairSettlement', function ($query) use ($condition, $date) {
            $query->whereBetween('FSettleDate', [$date['start_time'], $date['end_time']]);
            $query->where('FRepairTypeID', '=', ERP_REPAIR_TYPE_TWO);
            $query->where('FStatus', ERP_ORDER_STATUS_SETTLEMENT);
            $query->where('FSourceBillTypeID', ERP_SCM_BILLTYPE);
            $query->where('FOrgUnitID', $condition['store_erp_id']);
        })->whereHas('erpRwoRepairItemEntry', function ($query) use ($condition, $date) {
            $query->where('FPersonID', $condition['user_erp_id']);
            $query->where('FItemStatus', ERP_ITEM_STATUS_SIX);
        })
        ->sum('FTaxAmount');

        return $amount;
    }
}